package com.stock.holdyourhands.ui.holding

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import androidx.lifecycle.ViewModelProvider
import com.stock.holdyourhands.databinding.DialogAddHoldingBinding
import com.stock.holdyourhands.db.HoldingModel
import com.stock.holdyourhands.ui.base.ActivitySupport
import com.stock.holdyourhands.ui.detail.HoldingDetailViewModel
import com.stock.holdyourhands.util.CommonUtil

const val ADD_BUNDLE_KEY = "ADD_BUNDLE_KEY"
const val ORIGIN_HOLDING = "ORIGIN_HOLDING"

class AddHoldingActivity : ActivitySupport() {

    private val holdingViewModel by lazy {
        ViewModelProvider(this)[HoldingViewModel::class.java]
    }

    private val holdingDetailViewModel by lazy {
        ViewModelProvider(this)[HoldingDetailViewModel::class.java]
    }

    private var originHoldingModel: HoldingModel? = null



        override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding = DialogAddHoldingBinding.inflate(LayoutInflater.from(this))
        setContentView(binding.root)
        var holdingModel: HoldingModel? = null
        originHoldingModel = intent.getBundleExtra(ADD_BUNDLE_KEY)?.getParcelable(ORIGIN_HOLDING)
        originHoldingModel?.let {
            binding.etCode.setText(it.code)
            binding.etName.setText(it.name)
            binding.etPlanedAmount.setText(it.planedMoney.toString())
            binding.etPrice.setText(it.marketPrice.toString())
            binding.etCost.setText(it.holdingPrice.toString())
            binding.etNumber.setText(it.holdingQuantity.toString())
        }
        binding.btnAdd.setOnClickListener {

            val result = kotlin.runCatching {
                // 代码，名称，计划投资金额，计划投资数量，市价
                val code = binding.etCode.text.toString()
                val name = binding.etName.text.toString()
                val planedMoney = binding.etPlanedAmount.text.toString().toFloat()
                val marketPrice =
                    if (CommonUtil.isBlankString(binding.etPrice.text.toString())) 0F else binding.etPrice.text.toString()
                        .toFloat()

                // 现有持仓价格和股数
                val priceString =  binding.etCost.text.toString()
                val holdingPrice  = if (priceString.isBlank()) {
                    marketPrice
                } else priceString.toFloat()
                val holdingQuantity = if (binding.etNumber.text.toString().isBlank()) 0 else binding.etNumber.text.toString().toInt()

                if (holdingQuantity == 0) {
                    showToast("必须输入持仓数量")
                    return@setOnClickListener
                }

                holdingModel = if (originHoldingModel == null) {
                    HoldingModel(
                        null,
                        code = code,
                        name = name,
                        marketPrice = marketPrice,
                        planedMoney = planedMoney,
                        holdingPrice = holdingPrice,
                        holdingQuantity = holdingQuantity,
                        firstHoldingPrice = holdingPrice,
                        assertLowestPrice = binding.etLowestPrice.text.toString().toFloat()
                    )
                } else {
                    HoldingModel(
                        originHoldingModel!!.id,
                        code = code,
                        name = name,
                        marketPrice = marketPrice,
                        planedMoney = planedMoney,
                        holdingPrice = holdingPrice,
                        holdingQuantity = holdingQuantity,
                        firstHoldingPrice = holdingPrice,
                        assertLowestPrice = binding.etLowestPrice.text.toString().toFloat()
                    )
                }


            }.isSuccess
            if (result) {
                holdingModel?.let { it1 ->
                    holdingViewModel.addHolding(it1)
                }
                finish()
            }
        }
    }

    private fun testHolding(): HoldingModel {
        val model = HoldingModel(
            null,
            code = "000333",
            name = "美的集团",
            marketPrice = 66F,
            planedMoney = 50000F,
            holdingPrice = 66F,
            holdingQuantity = 100,
            firstHoldingPrice = 0F,
            assertLowestPrice = 59F
        )
        return model
    }

    companion object {
        fun addHoldingDialog(activity: Context?, originHoldingModel: HoldingModel? = null) {
            val intent = Intent(activity, AddHoldingActivity::class.java)
            val bundle = Bundle()
            bundle.putParcelable(ORIGIN_HOLDING, originHoldingModel)
            intent.putExtra(ADD_BUNDLE_KEY, bundle)
            activity?.startActivity(intent)
        }

    }
}